package study.springboot.mvc.support.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 要点：
 * （1） preHandle 方法决定
 * （2） postHandle 抛异常时
 * （3）
 * （4）
 */
@Slf4j
@Component
public class CustomHandlerInterceptor implements HandlerInterceptor {

    /**
     * 执行前处理
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        log.info(">>>>>> preHandle");
//        WebUtils.write(response, MapUtil.builder().put("111", "22").put("aaa", "bbb").build());
        return true;
    }

    /**
     * 执行后处理
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {
        log.info(">>>>>> postHandle");
    }

    /**
     * 完成后处理
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
        log.info(">>>>>> afterCompletion");
    }
}
